#include <compiler_defs.h>
#include <c8051f380_defs.h>
#include "USB0_ReportHandler.h"
#include "USB0_InterruptServiceRoutine.h"
//-------------------------------------------------------------------------

// ****************************************************************************
// Configure Setup_OUT_BUFFER
//
// Reminder:
// This function should set OUT_BUFFER.Ptr so that it
// points to an array in data space big enough to store
// any output report.
// It should also set OUT_BUFFER.Length to the size of
// this buffer.
//
// ****************************************************************************
unsigned char xdata OUT_PACKET[64];
unsigned char xdata IN_PACKET[64];
BufferStructure IN_BUFFER, OUT_BUFFER;

void Setup_OUT_BUFFER(void)
{
	OUT_BUFFER.Ptr = OUT_PACKET;
	OUT_BUFFER.Length = 63;
}

// ----------------------------------------------------------------------------
// ReportHandler_IN...
// ----------------------------------------------------------------------------
//
// Return Value - None
// Parameters - Report ID
//
// These functions match the Report ID passed as a parameter
// to an Input Report Handler.
// the ...FG function is called in the SendPacket foreground routine,
// while the ...ISR function is called inside the USB ISR.  A lock
// is set whenever one function is called to prevent a call from the
// other from disrupting the routine.
// However, this should never occur, as interrupts are disabled by SendPacket
// before USB operation begins.
// ----------------------------------------------------------------------------

void ReportHandler_IN_ISR(unsigned char R_ID)
{
	if (R_ID == 1)
	{ 
		IN_PACKET[0] = R_ID;
		IN_PACKET[1] = (P1 & 0x30) >> 4;
		IN_BUFFER.Ptr = IN_PACKET;
		IN_BUFFER.Length=2;
	}
}

void ReportHandler_IN_Foreground(unsigned char R_ID)
{
	if (R_ID == 1)
	{ 
		IN_PACKET[0] = R_ID;
		IN_PACKET[1] = (P1 & 0x30) >> 4;
		IN_BUFFER.Ptr = IN_PACKET;
		IN_BUFFER.Length=2;
	}
}

// ----------------------------------------------------------------------------
// ReportHandler_OUT
// ----------------------------------------------------------------------------
//
// Return Value - None
// Parameters - None
//
// This function matches the Report ID passed as a parameter
// to an Output Report Handler.
//
// ----------------------------------------------------------------------------
void ReportHandler_OUT()
{
	if (OUT_BUFFER.Ptr[0] == 2)
	{
  		unsigned char c;
		c = OUT_BUFFER.Ptr[1] << 4;
 		P1 = P1 ^ c;	
		SendPacket(1);
//		ReportHandler_IN_ISR(1);
	}
}